var BASE_URL = 'http://jinzhidata.com/v2'


$(function () {
    // 加载报告/资料详情
    // 获取资源id
    var url = window.location.href
    var resourceId = new RegExp(BASE_URL + "/pc/resource\\.html\\?resourceId=(.*)").exec(url)[1]
    // var resourceId = 'a7bc20b7b7d741cd967bb2da48f34208'
    var resourcePrice = -1
    // 加载资源详情
    $.ajax({
        url: BASE_URL + "/resource/" + resourceId,
        type: "get",
        contentType: 'application/json',
        dataType: "json",
        success: function (response) {
            var data = response.data
            resourcePrice = data.resourcePrice
            $("#resourcePrice").html(data.resourcePrice)
            $("#resourceName").html(data.resourceName)
            $("#createTime").html('发表于' + formatTime(data.createTime, "yyyy-MM-dd") + '&nbsp;&nbsp;&nbsp;&nbsp;')
            $("#resourceAuthorName").html(data.resourceAuthorName)
            // $("#resourceCover").attr("src", '/img/' + data.resourceCover)
            $("#resourceIntroduction").html(data.resourceIntroduction)
            $("#resourceDetail").html(data.resourceDetail)
        }
    })

    $.ajax({
        type: 'get',
        url: BASE_URL + '/order/getOrderState',
        async: false,
        data: {'resourceId': resourceId},
        success: function (result) {
            if (result.data === "0") {
                $("#buy").show()
                $("#download").hide()
            } else {
                $("#buy").hide()
                $("#download").show()
            }
        }
    });

    $("#buy").click(function () {
        if (!confirm("确定要使用" + resourcePrice + "积分购买该资源吗？")) {
            return;
        }
        $.ajax({
            type: 'get',
            url: BASE_URL + "/order/pay?resourceId=" + resourceId,
            async: false,
            success: function (result) {
                if (result.code === 0) {
                    window.location.reload()
                } else if (result.code === 1001) {
                    layer.msg('您还没有登录！', {icon: 2}, function () {
                        login()
                    })
                } else if (result.code === 5003) {
                    layer.msg('积分不足，请充值积分！', {icon: 2}, function () {
                        webCharge()
                    })
                } else {
                    alert(result.msg)
                }
            }
        });
    })

    $("#download").click(function () {
        location.href = BASE_URL + "/resource/download/" + resourceId
    })
})

